import threading

from model.cannonball_model import cannonBall
from utils.tkinter_image_utils import TkinterImageUtils
import tkinter as tk
class Tank:

    def __init__(self,base_window,img_path,x,y,width,height):
        self.pre_pressed_key = None
        self.pos_x=x
        self.pos_y=y
        self.width=width
        self.height=height
        self.img_path=img_path
        self.base_window=base_window

    def create_tank_ins(self,rotate_degree):
        tank_img = TkinterImageUtils.load_tk_img(self.img_path, rotate_degree)
        self.tank_label = tk.Label(self.base_window, image=tank_img)
        self.tank_label.place(x=self.pos_x, y=self.pos_y, width=self.width, height=self.height)
        self.base_window.mainloop()


    def on_key_pressed(self,pressed_key):
        if pressed_key==" ":
            print("点击了空格")
            self.fire()
        else:
            if pressed_key == self.pre_pressed_key:
                print("进行移动")
                self.pre_pressed_key=pressed_key
                self.move(pressed_key)
            else:
                print("改变方向")
                self.pre_pressed_key = pressed_key
                self.changeDirection(pressed_key)


    def move(self,key_pressed):
        if 'D'==key_pressed or 'd'==key_pressed:
            self.pos_x+=1
        elif "W"==key_pressed or "w"==key_pressed:
            self.pos_y-=1
        elif "A"==key_pressed or "a"==key_pressed:
            self.pos_x-=1
        elif "S"==key_pressed or "s"==key_pressed:
            self.pos_y+=1
        self.tank_label.place(x=self.pos_x, y=self.pos_y, width=self.width, height=self.height)

    def changeDirection(self,key_pressed):
        rotate_degree=0
        if 'D'==key_pressed or 'd'==key_pressed:
            rotate_degree=270
        elif "W"==key_pressed or "w"==key_pressed:
            pass
        elif "A"==key_pressed or "a"==key_pressed:
            rotate_degree = 90
        elif "S"==key_pressed or "s"==key_pressed:
            rotate_degree = 180

        self.tank_label.destroy()

        self.create_tank_ins(rotate_degree)

    def fire(self):

        cannon_ball=cannonBall(self.base_window,100,100,1)
        cannon_ball.update_connon_ball_position()
